package com.health.viewscontrols;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.health.R;
import com.health.adapter.MainMenuGridAdapter_NewTJContent;
import com.health.db.FDbManager;
import com.health.viewscontrols.focusgroup.ChildActivity;
import com.health.viewscontrols.focusgroup.GXYActivity;
import com.health.viewscontrols.focusgroup.JSJBctivity;
import com.health.viewscontrols.focusgroup.MaternityActivity;
import com.health.viewscontrols.focusgroup.NCZActivity;
import com.health.viewscontrols.focusgroup.OlderActivity;
import com.health.viewscontrols.focusgroup.TNBActivity;
import com.health.viewscontrols.photo.PhotoMainActivity2;
import com.health.tablebean.ARCHIVE_ASSESSMENTGUIDE;
import com.health.tablebean.ARCHIVE_BASEINFO;
import com.health.tablebean.SBCL_DATA;
import com.health.viewscontrols.tijian.TiJianActivity;
import com.novabluetooth.services.hqxd.HQXDActivity;
import com.novabluetooth.services.lbxd.ShowPicActivity;
import com.novabluetooth.views.ExamHistoryActivity;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

/**
 * 健康体检界面
 * 
 * @author nova
 * 
 */
public class MainActivity_Menu_NewTJ extends NovaFragmentActivity implements android.view.View.OnClickListener {

	private GridView gd_menu;
	private MainMenuGridAdapter_NewTJContent menuAdapter;
	protected ProgressDialog progressDialog;
	private ImageView img_back;

	private EditText ed_bloodglucose;// 血糖
	private EditText ed_weight;// 体重
	private EditText ed_ox;// 血氧
	private EditText ed_pluserate;// 脉率
	private EditText et_left_h;// 左 高压
	private EditText et_left_p;// 左 低压
	private EditText et_right_h;// 右 高压
	private EditText et_right_p;// 右 低压
	private EditText ed_tw;// 体温
	private EditText ed_xhdb;// 血红蛋白
	private Button btn_report_result;// 本次综合报告
	private Button btn_update;

	private TextView tv_doc_advice;
	
	
	/** 心电图 */
	private RadioGroup rg_ecg;
	private RadioButton rd_normal;
	private RadioButton rd_unnormal;
	private EditText et_xindiantu;
	private TextView tv_ecg;
	// private Button btn_xindianchongzhi;
	// private Button ecg_btn;

	/** 尿液分析 NiceSpinner TextView */
	private TextView tv_sg;
	private TextView tv_ph;
	private TextView tv_leu;
	private TextView tv_nit;
	private TextView tv_pro;
	private TextView tv_glu;
	private TextView tv_ket;
	private TextView tv_blo;
	private TextView tv_uro;
	private TextView tv_asc;
	private TextView tv_bil;
	private Button btn_cl_ecg;// 心电测量
	private Button btn_nj;// 尿液分析

	/**
	 * 人群分类
	 */
	public boolean isChild = false;// 儿童
	public boolean isPregnant = false;// 孕妇
	public boolean isOlder = false;// 老人
	public boolean isPsychosis = false;// 精神病
	public boolean isHypertension = false;// 高血压
	public boolean isDiabetes = false;// 糖尿病
	public boolean isCHD = false;// 冠心病
	public boolean isStroke = false;// 脑卒中
	public boolean isHandicapped = false;// 残疾人
	private ARCHIVE_BASEINFO userInfo = null;

	private ImageView img_camera;// 拍照

	private FDbManager fdbmanger = null;
	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	private int assfrom=-1;
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_tijian);
		userInfo = NovaApplication.getUserInfo();
		fdbmanger = NovaApplication.fdbmanger;
		getGroup();
		findViews();
		setListeners();
		getTjData();
		getYSJYData();
	}

	/**
	 * 获取人群分类
	 * 
	 * @author 王婷婷
	 * @date 2017年1月17日 下午4:39:46
	 */
	public void getGroup() {
		List<String> popList = new ArrayList<String>();
		if (!TextUtils.isEmpty(userInfo.getPOPULATION_TYPE())) {
			String popStr = NovaApplication.getUserInfo().getPOPULATION_TYPE();
			String[] pops = popStr.split(",");
			popList = Arrays.asList(pops);
		}
		isChild = popList.contains("2");
		isPregnant = popList.contains("3");
		isOlder = popList.contains("4");
		isPsychosis = popList.contains("5");
		isHypertension = popList.contains("6");
		isDiabetes = popList.contains("7");
		isCHD = popList.contains("8");
		isStroke = popList.contains("9");
		isHandicapped = popList.contains("10");
		// isChild = true;
		// isPregnant = true;
		// isOlder = true;
		// isPsychosis = true;
		// isHypertension = true;
		// isDiabetes = true;
		// isCHD = true;
		// isStroke = true;
		// isHandicapped = true;

	}

	public void findViews() {

		gd_menu = (GridView) findViewById(R.id.gd_menu);
		menuAdapter = new MainMenuGridAdapter_NewTJContent(getApplicationContext());
		gd_menu.setAdapter(menuAdapter);
		img_back = (ImageView) findViewById(R.id.img_back);
		img_back.setOnClickListener(this);

		ed_bloodglucose = (EditText) findViewById(R.id.ed_bloodglucose);
		ed_weight = (EditText) findViewById(R.id.ed_weight);

		ed_ox = (EditText) findViewById(R.id.ed_ox);
		ed_pluserate = (EditText) findViewById(R.id.ed_pluserate);
		et_left_h = (EditText) findViewById(R.id.et_left_h);
		et_left_p = (EditText) findViewById(R.id.et_left_p);
		et_right_h = (EditText) findViewById(R.id.et_right_h);
		et_right_p = (EditText) findViewById(R.id.et_right_p);

		ed_tw = (EditText) findViewById(R.id.ed_tw);
		ed_xhdb = (EditText) findViewById(R.id.ed_xhdb);

		tv_doc_advice=(TextView)findViewById(R.id.tv_doc_advice);
		/** 心电图 */
		rg_ecg = (RadioGroup) findViewById(R.id.rg_ecg);
		rd_normal = (RadioButton) findViewById(R.id.rd_normal);
		rd_unnormal = (RadioButton) findViewById(R.id.rd_unnormal);
		et_xindiantu = (EditText) findViewById(R.id.et_xindiantu);
		tv_ecg = (TextView) findViewById(R.id.tv_ecg);

		/** 尿液分析 */
		tv_sg = (TextView) findViewById(R.id.tv_sg);
		tv_ph = (TextView) findViewById(R.id.tv_ph);
		tv_leu = (TextView) findViewById(R.id.tv_leu);
		tv_nit = (TextView) findViewById(R.id.tv_nit);
		tv_pro = (TextView) findViewById(R.id.tv_pro);
		tv_glu = (TextView) findViewById(R.id.tv_glu);
		tv_ket = (TextView) findViewById(R.id.tv_ket);
		tv_blo = (TextView) findViewById(R.id.tv_blo);
		tv_bil = (TextView) findViewById(R.id.tv_bil);
		tv_uro = (TextView) findViewById(R.id.tv_uro);
		tv_asc = (TextView) findViewById(R.id.tv_asc);

		btn_cl_ecg = (Button) findViewById(R.id.btn_cl_ecg);
		btn_nj = (Button) findViewById(R.id.btn_nj);
		btn_cl_ecg.setOnClickListener(this);
		btn_nj.setOnClickListener(this);

		img_camera = (ImageView) findViewById(R.id.img_camera);
		img_camera.setOnClickListener(this);

		btn_report_result = (Button) findViewById(R.id.btn_report_result);
		btn_update = (Button) findViewById(R.id.btn_update);
		btn_report_result.setOnClickListener(this);
		btn_update.setOnClickListener(this);
	}

	public void setListeners() {
		// 菜单选项
		gd_menu.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// 0 "健康体检",
				// 1 "孕妇",
				// 2 "儿童",
				// 3 "老年人",
				// 4 "高血压",
				// 5 "个人档案",
				// 6 "糖尿病"
				// 7 "精神疾病"
				// 8 "脑卒中"
				// 9 "冠心病"
				Intent intent = null;
				if (position == 0) {
					// 0 "健康体检",
					intent = new Intent(getApplicationContext(), TiJianActivity.class);
				} else if (position == 1) {
					// 1 "孕妇",
					if (isPregnant) {
						intent = new Intent(getApplicationContext(), MaternityActivity.class);
					} else {
						showTextDialog("您不是孕妇");
					}
				} else if (position == 2) {
					// 2 "儿童",
					if (isChild) {
						intent = new Intent(getApplicationContext(), ChildActivity.class);
					} else {
						showTextDialog("您不是儿童");
					}
				} else if (position == 3) {
					// 3 "老年人",
					if (isOlder) {
						intent = new Intent(getApplicationContext(), OlderActivity.class);
					} else {
						showTextDialog("您不是老年人");
					}
				} else if (position == 4) {
					// 4 "高血压",
					if (isHypertension) {
						intent = new Intent(getApplicationContext(), GXYActivity.class);
					} else {
						showTextDialog("您不是高血压患者");
					}
				} else if (position == 5) {
					// 4 "个人档案",
					intent = new Intent(getApplicationContext(), PersonalFileActivity.class);
				} else if (position == 6) {
					// 5 "糖尿病",
					if (isDiabetes) {
						intent = new Intent(getApplicationContext(), TNBActivity.class);
					} else {
						showTextDialog("您不是糖尿病患者");
					}

				} else if (position == 7) {
					// 6 "精神疾病",
					if (isPsychosis) {
						intent = new Intent(getApplicationContext(), JSJBctivity.class);
					} else {
						showTextDialog("您不是精神疾病患者");
					}
				} else if (position == 8) {
					// 7 "脑卒中",
					if (isStroke) {
						intent = new Intent(getApplicationContext(), NCZActivity.class);
					} else {
						showTextDialog("您不是脑卒中患者");
					}
				} else if (position == 9) {
					// 7 "冠心病",
					if (isCHD) {
						intent = new Intent(getApplicationContext(), NCZActivity.class);
					} else {
						showTextDialog("您不是冠心病患者");
					}
				}
				if (intent != null) {
					startActivity(intent);
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.img_back:
			saveYSJYData();
			finish();
			break;
		case R.id.btn_cl_ecg:
			// 检测心电
			Intent intent = new Intent(getApplicationContext(), HQXDActivity.class);
			startActivity(intent);
			break;
		case R.id.btn_nj:
			// 尿液分析

			break;
		case R.id.img_camera:
			// 拍照
			intent = new Intent(getApplicationContext(), PhotoMainActivity2.class);
			startActivity(intent);
			break;
		// case R.id.btn_xindianchongzhi:
		// // 重置
		// rd_normal.setChecked(false);
		// rd_unnormal.setChecked(false);
		// et_xindiantu.setText("");
		//
		// break;
		case R.id.btn_report_result:
			// 本次综合报告
			intent = new Intent(getApplicationContext(), ShowJieLunPicActivity2.class);
			String str_xt=ed_bloodglucose.getText().toString();
			String str_xyLeft=et_left_h.getText().toString();
			String str_xyRight=et_left_p.getText().toString();
			String str_tw=ed_tw.getText().toString();
			String str_ml=ed_pluserate.getText().toString();
			String str_ox=ed_ox.getText().toString();
			String str_ysjy=tv_doc_advice.getText().toString();
			intent.putExtra("str_xt", str_xt);
			intent.putExtra("str_xyLeft",str_xyLeft);
			intent.putExtra("str_xyRight",str_xyRight);
			intent.putExtra("str_tw", str_tw);
			intent.putExtra("str_ml", str_ml);
			intent.putExtra("str_ox", str_ox);
			intent.putExtra("str_ysjy", str_ysjy);
			startActivity(intent);
			break;
		case R.id.btn_update:
			// 添加修改医生建议
			setDocAdviceDialog();
			break;
		default:
			break;
		}

	}

	/**
	 * 获取当日体检数据并设置数据
	 * 
	 * @author 王婷婷
	 * @date 2017年2月8日 下午2:17:21
	 */
	private void getTjData() {
		String currentDate = dateFormat.format(new Date());
		String sqlDataFront = "IDCARD = '" + userInfo.getIDCARD() + "' and CL_TYPE='";
		String sqlDataLatter = "' and " + "CL_TIME like '" + currentDate + "%'";
		// 血糖
		List<SBCL_DATA> xtList = fdbmanger.findAllByWhere(SBCL_DATA.class, sqlDataFront + "血糖" + sqlDataLatter);// mmol/L
		// 体重
		List<SBCL_DATA> tzList = fdbmanger.findAllByWhere(SBCL_DATA.class, sqlDataFront + "体重" + sqlDataLatter);// Kg
		// 血氧
		List<SBCL_DATA> xyList = fdbmanger.findAllByWhere(SBCL_DATA.class, sqlDataFront + "血氧" + sqlDataLatter);// %
		// 脉率
		List<SBCL_DATA> mlList = fdbmanger.findAllByWhere(SBCL_DATA.class, sqlDataFront + "脉率" + sqlDataLatter);// bpm

		// 体温
		List<SBCL_DATA> twList = fdbmanger.findAllByWhere(SBCL_DATA.class, sqlDataFront + "体温" + sqlDataLatter);// ℃
		// 血红蛋白
		List<SBCL_DATA> xhdbList = fdbmanger.findAllByWhere(SBCL_DATA.class, sqlDataFront + "血红蛋白" + sqlDataLatter);
		// 高压
		List<SBCL_DATA> gyList = fdbmanger.findAllByWhere(SBCL_DATA.class, sqlDataFront + "高压" + sqlDataLatter);// mmHg
		// 低压
		List<SBCL_DATA> dyList = fdbmanger.findAllByWhere(SBCL_DATA.class, sqlDataFront + "低压" + sqlDataLatter);// mmHg

		// 血糖
		if (xtList.size() > 0) {
			ed_bloodglucose.setText(xtList.get(xtList.size() - 1).getCL_VALUE().split("mmol/L")[0].trim());
		}
		// 体重
		if (tzList.size() > 0) {
			ed_weight.setText(tzList.get(tzList.size() - 1).getCL_VALUE().split("Kg")[0].trim());
		}
		// 血氧
		if (xyList.size() > 0) {
			ed_ox.setText(xyList.get(xyList.size() - 1).getCL_VALUE().split("%")[0].trim());
		}
		// 脉率
		if (mlList.size() > 0) {
			ed_pluserate.setText(mlList.get(mlList.size() - 1).getCL_VALUE().split("bpm")[0].trim());
		}

		// 体温
		if (twList.size() > 0) {
			ed_tw.setText(twList.get(twList.size() - 1).getCL_VALUE().split("℃")[0].trim());
		}
		// 血红蛋白
		if (xhdbList.size() > 0) {
			ed_xhdb.setText(xhdbList.get(xhdbList.size() - 1).getCL_VALUE());
		}

		// 左 高压
		if (gyList.size() > 0) {
			et_left_h.setText(gyList.get(gyList.size() - 1).getCL_VALUE().split("mmHg")[0].trim());
		}
		// 左 低压
		if (dyList.size() > 0) {
			et_left_p.setText(dyList.get(dyList.size() - 1).getCL_VALUE().split("mmHg")[0].trim());
		}

		// 右 高压
		if (gyList.size() - 1 > 0) {
			et_right_h.setText(gyList.get(gyList.size() - 2).getCL_VALUE().split("mmHg")[0].trim());
		}
		// 右 低压
		if (dyList.size() - 1 > 0) {
			et_right_p.setText(dyList.get(dyList.size() - 2).getCL_VALUE().split("mmHg")[0].trim());
		}

		// 尿液分析
		// 尿胆原
		List<SBCL_DATA> nj_uroList = fdbmanger.findAllByWhere(SBCL_DATA.class, sqlDataFront + "尿胆原" + sqlDataLatter);
		// 潜血
		List<SBCL_DATA> nj_bloList = fdbmanger.findAllByWhere(SBCL_DATA.class, sqlDataFront + "潜血" + sqlDataLatter);
		// 胆红素
		List<SBCL_DATA> nj_bilList = fdbmanger.findAllByWhere(SBCL_DATA.class, sqlDataFront + "胆红素" + sqlDataLatter);
		// 酮体
		List<SBCL_DATA> nj_ketList = fdbmanger.findAllByWhere(SBCL_DATA.class, sqlDataFront + "酮体" + sqlDataLatter);
		// 葡萄糖
		List<SBCL_DATA> nj_gluList = fdbmanger.findAllByWhere(SBCL_DATA.class, sqlDataFront + "葡萄糖" + sqlDataLatter);
		// 蛋白质
		List<SBCL_DATA> nj_proList = fdbmanger.findAllByWhere(SBCL_DATA.class, sqlDataFront + "蛋白质" + sqlDataLatter);

		// PH
		List<SBCL_DATA> nj_phList = fdbmanger.findAllByWhere(SBCL_DATA.class, sqlDataFront + "酸碱度" + sqlDataLatter);
		// 亚硝酸盐
		List<SBCL_DATA> nj_nitList = fdbmanger.findAllByWhere(SBCL_DATA.class, sqlDataFront + "亚硝酸盐" + sqlDataLatter);
		// 白细胞
		List<SBCL_DATA> nj_leuList = fdbmanger.findAllByWhere(SBCL_DATA.class, sqlDataFront + "白细胞" + sqlDataLatter);
		// 比重
		List<SBCL_DATA> nj_sgList = fdbmanger.findAllByWhere(SBCL_DATA.class, sqlDataFront + "比重" + sqlDataLatter);
		// 维生素
		List<SBCL_DATA> nj_ascList = fdbmanger.findAllByWhere(SBCL_DATA.class, sqlDataFront + "维生素" + sqlDataLatter);

		if (nj_uroList.size() > 0) {

			tv_sg.setText(nj_sgList.get(nj_sgList.size() - 1).getCL_VALUE());// 比重
			tv_ph.setText(nj_phList.get(nj_phList.size() - 1).getCL_VALUE());// ph
			tv_leu.setText(nj_leuList.get(nj_leuList.size() - 1).getCL_VALUE());// 白细胞
			tv_glu.setText(nj_gluList.get(nj_gluList.size() - 1).getCL_VALUE());// 葡萄糖
			tv_ket.setText(nj_ketList.get(nj_ketList.size() - 1).getCL_VALUE());// 酮体
			tv_blo.setText(nj_bloList.get(nj_bloList.size() - 1).getCL_VALUE());// 比重
			tv_nit.setText(nj_nitList.get(nj_nitList.size() - 1).getCL_VALUE());// 亚硝酸盐
			tv_pro.setText(nj_proList.get(nj_proList.size() - 1).getCL_VALUE());// 蛋白质
			tv_bil.setText(nj_bilList.get(nj_bilList.size() - 1).getCL_VALUE());// 胆红素
			tv_asc.setText(nj_ascList.get(nj_ascList.size() - 1).getCL_VALUE());// 维生素
			tv_uro.setText(nj_uroList.get(nj_uroList.size() - 1).getCL_VALUE());// 尿胆原
		}

		// 心电
		List<SBCL_DATA> ecgList = fdbmanger.findAllByWhere(SBCL_DATA.class,
				sqlDataFront + "理邦心电诊断结果" + sqlDataLatter);
		if (ecgList.size() > 0) {
			String str_result = ecgList.get(ecgList.size() - 1).getCL_VALUE();
			et_xindiantu.setText(str_result);
			if (str_result.contains("正常")) {
				rd_normal.setChecked(true);
			} else {
				rd_unnormal.setChecked(true);
			}
			String str_time = ecgList.get(ecgList.size() - 1).getCL_TIME();
			final String jpg_url = ExamHistoryActivity.getXinDianPicture(str_time);
			if (jpg_url != null) {
				tv_ecg.setTextColor(Color.BLUE);
				tv_ecg.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
				// 心电图点击事件
				tv_ecg.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent intent = new Intent(getApplicationContext(), ShowPicActivity.class);
						intent.putExtra("JPG_URL", jpg_url);
						startActivity(intent);
					}
				});
			}

		}
	}

//	@Override
//	public void showTJDialog(String text) {
//		// TODO Auto-generated method stub
//		super.showTJDialog(text);
//		getTjData();
//	}

	/**
	 * 设置医生建议添加框
	 * 
	 * @author 王婷婷
	 * @date 2017年2月15日 下午4:55:50
	 */
	private void setDocAdviceDialog() {
		Builder builder = new AlertDialog.Builder(this);
		LayoutInflater factory = LayoutInflater.from(this);
		View docView = factory.inflate(R.layout.dialog_with_editbotton, null);
		final EditText ed_doc_advice = (EditText) docView.findViewById(R.id.ed_doc_advice);

		Button btn_cancel = (Button) docView.findViewById(R.id.btn_cancel);
		Button btn_save = (Button) docView.findViewById(R.id.btn_save);
		
		String str_edAdvice=tv_doc_advice.getText().toString();
		if(TextUtils.isEmpty(str_edAdvice)){
		}else{
			ed_doc_advice.setText(str_edAdvice);
		}
		
		builder.setView(docView);
		final AlertDialog dialog = builder.create();
		btn_cancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog.dismiss();
				
			}
		});
		btn_save.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String str_doc = ed_doc_advice.getText().toString();
				if (TextUtils.isEmpty(str_doc)) {
					showTextDialog("请输入医生建议");
				}else{
					tv_doc_advice.setText(str_doc);
					dialog.dismiss();
				}
			}
		});
		
	
		
		Window window = dialog.getWindow();
		window.setGravity(Gravity.CENTER); // 此处可以设置dialog显示的位置
		dialog.show();

	}
	/**
	 * 设置医生建议
	 * 
	 * @author 王婷婷
	 * @date 2017年2月18日 下午3:11:20
	 */
	private void getYSJYData(){
		List<ARCHIVE_ASSESSMENTGUIDE> asss = fdbmanger.findAllByWhere(ARCHIVE_ASSESSMENTGUIDE.class, "IDCARD = '" + userInfo.getIDCARD() + "'");
		if (asss.size() == 0) {
			assfrom=0;
			return;
		}
		ARCHIVE_ASSESSMENTGUIDE ass = asss.get(asss.size() - 1);
		tv_doc_advice.setText(ass.getOTHER());
	}
	/**
	 * 保存医生建议
	 * 
	 * @author 王婷婷
	 * @date 2017年2月18日 下午3:11:20
	 */
	private void saveYSJYData(){
		String str_doc_advice=tv_doc_advice.getText().toString();
		if(!TextUtils.isEmpty(str_doc_advice)){
			ARCHIVE_ASSESSMENTGUIDE ass = new ARCHIVE_ASSESSMENTGUIDE();
			ass.setIDCARD(userInfo.getIDCARD());
			ass.setOTHER(str_doc_advice);
			if (assfrom == 0) {
				fdbmanger.save(ass);
				assfrom = 1;
			} else {
				fdbmanger.updata(ass, "IDCARD='" + userInfo.getIDCARD() + "'");
			}
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		 if(keyCode==KeyEvent.KEYCODE_BACK){
			 saveYSJYData();
			 finish();
		 }
		return super.onKeyDown(keyCode, event);
	}
}
